package controllers

import (
	"fmt"
	"github.com/beego/beego/v2/core/validation"
	beego "github.com/beego/beego/v2/server/web"
)

// ValidationController operations for Validation
type ValidationController struct {
	beego.Controller
}

type User struct {
	Name string
	Age  int
}

// URLMapping ...
func (c *ValidationController) URLMapping() {
	c.Mapping("Post", c.Post)
	c.Mapping("GetOne", c.GetOne)
	c.Mapping("GetAll", c.GetAll)
	c.Mapping("Put", c.Put)
	c.Mapping("Delete", c.Delete)
}

// Post ...
// @Title Create
// @Description create Validation
// @Param	body		body 	models.Validation	true		"body for Validation content"
// @Success 201 {object} models.Validation
// @Failure 403 body is empty
// @router / [post]
func (c *ValidationController) Post() {

}

// GetOne ...
// @Title GetOne
// @Description get Validation by id
// @Param	id		path 	string	true		"The key for staticblock"
// @Success 200 {object} models.Validation
// @Failure 403 :id is empty
// @router /:id [get]
func (c *ValidationController) GetOne() {

}

// GetAll ...
// @Title GetAll
// @Description get Validation
// @Param	query	query	string	false	"Filter. e.g. col1:v1,col2:v2 ..."
// @Param	fields	query	string	false	"Fields returned. e.g. col1,col2 ..."
// @Param	sortby	query	string	false	"Sorted-by fields. e.g. col1,col2 ..."
// @Param	order	query	string	false	"Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param	limit	query	string	false	"Limit the size of result set. Must be an integer"
// @Param	offset	query	string	false	"Start position of result set. Must be an integer"
// @Success 200 {object} models.Validation
// @Failure 403
// @router / [get]
func (c *ValidationController) GetAll() {

}

// Put ...
// @Title Put
// @Description update the Validation
// @Param	id		path 	string	true		"The id you want to update"
// @Param	body		body 	models.Validation	true		"body for Validation content"
// @Success 200 {object} models.Validation
// @Failure 403 :id is not int
// @router /:id [put]
func (c *ValidationController) Put() {
	//表单数据验证
	age, _ := c.GetInt("age", 0)

	var u = &User{
		Name: c.GetString("name", ""),
		Age:  age,
	}
	fmt.Println(u)

	valid := validation.Validation{}
	valid.Required(u.Name, "name")
	valid.Required(u.Age, "age")
	valid.MaxSize(u.Name, 15, "name")
	valid.Range(u.Age, 0, 18, "age").Message("0~18岁之间！")

	fmt.Println("HasErrors: ", valid.HasErrors())

	//或像这样使用
	/*if v := valid.Max(u.Age, 100, "age"); !v.Ok {
		//log.Println(v.Error.Key, v.Error.Message)
		c.Data["json"] = v.Error.Key + "：" + v.Error.Message
	}*/

	//定制化错误
	var minAge = 18
	valid.Min(u.Age, minAge, "age").Message("少儿不宜！")
	// 错误信息格式化
	//valid.Min(u.Age, minAge, "age").Message("%d不禁", minAge)

	if valid.HasErrors() {
		// 如果有错误信息，证明验证没通过
		// 打印错误信息

		for _, err := range valid.Errors {
			//log.Println(err.Key, err.Message)
			c.Data["json"] = err.Key + "：" + err.Message
		}

	} else {
		c.Data["json"] = u
	}

	c.ServeJSON()
}

// Delete ...
// @Title Delete
// @Description delete the Validation
// @Param	id		path 	string	true		"The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *ValidationController) Delete() {

}
